import { tips } from '@/utils/notification'
export default {
  data() {
    return {
      search: {}, // 头部搜索部分
      dialogFormVisible: false,
      total: 0,
      listQuery: {
        page: 1,
        size: 10
      },
      tableData: [],
      options: {
        stripe: true,
        align: 'center',
        loading: true,
        selection: false, // 时候否展示选中框
        showIndex: true, // 是否展示序号
        showTooltip: true // 是否当内容过长被隐藏时显示 tooltip
      }, // 配置项
      operation: {
        label: '',
        isShow: false
      }, // 操作项
      tableColumns: [],
      // 是否禁用按钮
      UpdateDisabled: true,
      DeleteDisabled: true,
      multipleSelection: []
    }
  },
  watch: {
    // 监听选中数量
    multipleSelection: {
      handler() {
        if (this.multipleSelection.length > 0) {
          if (this.multipleSelection.length === 1) {
            this.UpdateDisabled = false
          } else {
            this.UpdateDisabled = true
          }
          this.DeleteDisabled = false
        } else {
          this.UpdateDisabled = true
          this.DeleteDisabled = true
        }
      }
    }
  },
  methods: {
    // 切换分页
    getList(val) {
      this.listQuery = val
      this.initList()
    },
    setOnePage() {
      // 删除成功直接变成第一页, 不搞其他乱七八糟的
      // const lastPage = Math.ceil((this.total - 1) / this.listQuery.size)
      // const currentPage = this.listQuery.page > lastPage ? lastPage : this.listQuery.page
      // this.listQuery.page = currentPage < 1 ? 1 : currentPage
      this.listQuery.page = 1
    },
    replaceStr(str, arr) {
      const regArr = str.match(/\-\d\-/g)
      let temp = str
      // 只有匹配上了才进行字符串操作,否则直接返回原字符串
      if (regArr) {
        for (let i = 0; i < regArr.length; i++) {
          temp = temp.replace(new RegExp(regArr[i], 'g'), arr[i])
        }
        return temp
      } else {
        return str
      }
    },
    // 合并操作
    doMerge(val) {
      // 成功的话
      if (val.code === 1) {
        this.dialogFormVisible = false
        this.$emit('doSave')
        this.$emit('closeDialog')
        tips({ message: this.$t(val.message), code: val.code })
      } else if (val.code === 2) {
        tips({ message: this.replaceStr(this.$t(val.message), val.data), code: val.code })
        return
      } else {
        tips({ message: this.$t(val.message), code: val.code })
        return
      }
    },
    // 验证表单
    validationForm(val, cb) {
      this.$refs[val].validate((valid) => {
        if (!valid) {
          return false
        }
        cb()
      })
    },
    // 提交表单
    submitForm(val) {
      if (val.code === 1) {
        Object.assign(this.$data, this.$options.data.call(this))
        tips({ message: this.$t(val.message), code: val.code })
      } else if (val.code === 2) {
        tips({ message: this.$t(val.message), code: val.code })
      } else {
        tips({ message: this.$t(val.message), code: val.code })
        return
      }
    },
    // 当某一行被点击时
    rowClick(row) {
      this.multipleSelection = row
    },
    // 简单的el-date-picker值的处理
    handleTime(start, end, time) {
      if (time) {
        return {
          [start]: time[0],
          [end]: time[1]
        }
      } else {
        return {
          [start]: null,
          [end]: null
        }
      }
    }
  }
}
